package com.tuling.proxy;

import com.tuling.proxy.advice.BeforeMethodInterceptor;
import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.NameMatchMethodPointcut;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;

@Component
@Import(DefaultAdvisorAutoProxyCreator.class)
public class ProxyCreaterRelation {
    /**
     * 绑定spring 中的两个bean beanName user* 匹配的会执行前置处理
     * @return
     */
//    @Bean
//    public BeanNameAutoProxyCreator beanNameAutoProxyCreator(){
//        BeanNameAutoProxyCreator beanNameAutoProxyCreator = new BeanNameAutoProxyCreator();
//        beanNameAutoProxyCreator.setBeanNames("user*");
//        beanNameAutoProxyCreator.setInterceptorNames("beforeMethodInterceptor");
//        beanNameAutoProxyCreator.setProxyTargetClass(true);
//        return beanNameAutoProxyCreator;
//    }

    /**
     *  切入点+ 切面逻辑
     * @return
     */
    @Bean
    public DefaultPointcutAdvisor defaultPointcutAdvisor(){
        NameMatchMethodPointcut pointcut = new NameMatchMethodPointcut();
        pointcut.addMethodName("test");
        DefaultPointcutAdvisor defaultPointcutAdvisor = new DefaultPointcutAdvisor();
        defaultPointcutAdvisor.setPointcut(pointcut);
        defaultPointcutAdvisor.setAdvice(new BeforeMethodInterceptor());
        return defaultPointcutAdvisor;
    }
    /**
     * DefaultAdvisorAutoProxyCreator  在bean 生命周期初始化后的时候会寻找当前是不是存在advisor bean
     */
//    @Bean
//    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator(){
//        DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
//        return defaultAdvisorAutoProxyCreator;
//    }
}
